package com.guigu.dealservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.R;
import com.guigu.dealservice.dto.Equipment;
import com.guigu.dealservice.dto.User;
import com.guigu.dealservice.entity.MonthlyData;
import com.guigu.dealservice.entity.Order;
import com.guigu.dealservice.feign.MyEquipmentFeign;
import com.guigu.dealservice.feign.MyUserFeign;
import com.guigu.dealservice.mapper.OrderMapper;
import com.guigu.dealservice.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.dealservice.vo.OrderVo;
import com.guigu.dealservice.vo.PageOrderVoPC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 订单表（存储买卖双方的交易订单信息） 服务实现类
 * </p>
 *
 * @author ysp
 * @since 2025-10-10
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MyUserFeign myUserFeign;
    @Autowired
    private MyEquipmentFeign myEquipmentFeign;
    @Override
    public List<Order> queryOrderByUserId(Long userId) {
        List<Order> orders = orderMapper.queryOrderByUserId(userId);
        return  orders;
    }

    @Override
    public R queryOrderListByVo(Integer pageNum, Integer pageSize, OrderVo vo) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(vo.getBuyerId() != null, "buyer_id", vo.getBuyerId());
        queryWrapper.like(vo.getSellerId() != null, "seller_id", vo.getSellerId());
        queryWrapper.like(vo.getEquipment() != null, "equipment_id", vo.getEquipment());
        queryWrapper.like(vo.getStatus() != null, "order_status", vo.getStatus());
        if (vo.getTimeCreate() != null && vo.getTimeCreate().size() >= 2) {
            queryWrapper.ge(vo.getTimeCreate().get(0) != null , "create_time", vo.getTimeCreate().get(0));
            queryWrapper.le(vo.getTimeCreate().get(1) != null , "create_time", vo.getTimeCreate().get(1));
        }
        List<Order> orders = orderMapper.selectList(queryWrapper);
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        for (Order o:orders) {
            Equipment equipment = myEquipmentFeign.selectEquipmentByIdFeign(o.getEquipmentId());
            o.setEquipmentOne(equipment);
            User buyerUser = myUserFeign.queryUserById(Math.toIntExact(o.getBuyerId()));
            o.setBuyerUser(buyerUser);
            User sellerUser = myUserFeign.queryUserById(Math.toIntExact(o.getSellerId()));
            o.setSellerUser(sellerUser);
        }
        return R.ok().data("pageInfo",pageInfo);
    }

    @Override
    public R queryOrderById(Long orderId) {
        Order order = orderMapper.selectById(orderId);
        if (order != null) {
            Equipment equipment = myEquipmentFeign.selectEquipmentByIdFeign(order.getEquipmentId());
            order.setEquipmentOne(equipment);
            User buyerUser = myUserFeign.queryUserById(Math.toIntExact(order.getBuyerId()));
            order.setBuyerUser(buyerUser);
            User sellerUser = myUserFeign.queryUserById(Math.toIntExact(order.getSellerId()));
            order.setSellerUser(sellerUser);
        }
        return R.ok().data("order",order);
    }

    @Override
    public R queryOrderByBuyerId(Integer pageNum, Integer pageSize, PageOrderVoPC vo) {

        return null;
    }

    @Override
    public List<MonthlyData> getRecentSixMonthsData() {
        return orderMapper.getRecentSixMonthsData();
    }


}
